/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.servlet;

import cn.webrx.db.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: jdbc202202 - ShowServlet
 * <p>Powered by webrx On 2022-08-23 10:55:03
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@WebServlet("/show")
public class ShowServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DbUtil du = new DbUtil();
        du.setPagesize(1);
        //List<Map<String, Object>> ts = du.query("t_student");
        int currpage = req.getParameter("p") == null ? 1 : Integer.parseInt(req.getParameter("p"));

        List<Map<String, Object>> ts = du.page(currpage, "t_student","*","where 1=1", "order by create_time desc");

        req.setAttribute("data", ts);
        req.setAttribute("cp",du.getCurrpage());
        req.setAttribute("ps",du.getPagesize());
        req.setAttribute("rc",du.getRecordcount());
        req.setAttribute("pc",du.getPagecount());
        req.setAttribute("pageinfo",du.pagebootstrap());

        req.getRequestDispatcher("show.jsp").forward(req, resp);
    }

    protected void abc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DbUtil du = new DbUtil();
        List<Map<String, Object>> ts = du.query("t_student");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<meta charset='utf-8'><style>img{width:35px;}</style>");
        int index = 0;
        for (Map<String, Object> t : ts) {
            out.print((++index) + "、" + t.get("name") + "<br>");
            out.print("<img src=" + t.get("photo") + ">");

        }
        out.flush();
        out.close();
    }
}
